<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['userName']))
	exit();

function searchABMPtoSuelta()
{
	
	require("includes/constants.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('idPtoSuelta, description, codPos, province, latitude, longitude, kms')
			->from('ptosuelta')
			->where('state <>', $stateErase)
			->edit_column('latitude', '$1', 'transformLatLng(latitude)') // php functions
			->edit_column('longitude', '$1', 'transformLatLng(longitude)'); // php functions
					
	return $datatables->generate();
}

function transformLatLng($decimalVal)
{
	$signVal = 1;
			
	$valAbs = abs(round($decimalVal * 1000000.));
	$valDeg = (floor($valAbs / 1000000) * $signVal);		
	$valMin = floor((($valAbs/1000000) - floor($valAbs/1000000)) * 60);
	$valSeg = (floor((((($valAbs/1000000) - floor($valAbs/1000000)) * 60) - floor((($valAbs/1000000) - floor($valAbs/1000000)) * 60)) * 100000) *60/100000);
	
	return $valDeg.'&deg;'.$valMin.'\''.(int)$valSeg.'"';
}

function addOrEdit()
{
	require("includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	//Latitud
	$latSign = 1;
	if($_REQUEST['latDeg'] < 0) 
		 $latSign = -1;
	$absdlat = abs(round($_REQUEST['latDeg'] * 1000000.));
	$absmlat = abs(round($_REQUEST['latMin'] * 1000000.)/1000000);
	$absslat = abs(round($_REQUEST['latSeg'] * 1000000.)/1000000);
	
	$absmlat = abs(round($absmlat * 1000000.));
	$absslat = abs(round($absslat * 1000000.)); 

	//Longitud
	$lngSign = 1;
	if($_REQUEST['lngDeg'] < 0) 
		 $lngSign = -1;
	$absdlng = abs(round($_REQUEST['lngDeg'] * 1000000.));
	$absmlng = abs(round($_REQUEST['lngMin'] * 1000000.)/1000000);
	$absslng = abs(round($_REQUEST['lngSeg'] * 1000000.)/1000000);
	
	$absmlng = abs(round($absmlng * 1000000.));
	$absslng = abs(round($absslng * 1000000.)); 

	$latitude = round($absdlat + ($absmlat/60.) + ($absslat/3600.) ) * $latSign/1000000;
	$longitude = round($absdlng + ($absmlng/60.) + ($absslng/3600.) ) * $lngSign/1000000;

	$DB->StartTrans(); // start transaction
	
	if(!isset($_REQUEST["idPtoSuelta"])){
		$strSQL = "INSERT INTO ptosuelta ( ";
		$strSQL .= "	description, ";
		$strSQL .= "	codPos, ";
		$strSQL .= "	province, ";
		$strSQL .= "	latitude, ";
		$strSQL .= "	longitude, ";
		$strSQL .= "	kms, ";
		$strSQL .= "	weatherCode, ";
		$strSQL .= "	state ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["description"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["codPos"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["province"], "UTF-8")."', ";
		$strSQL .= "	".$latitude.", ";
		$strSQL .= "	".$longitude.", ";
		$strSQL .= "	".$_REQUEST["kmts"].", ";
		$strSQL .= "	".$_REQUEST["weatherLocation"].", ";
		$strSQL .= "	'".$stateAdd."'";
		$strSQL .= "	)";
	}
	else{
		$strSQL = "UPDATE ptosuelta SET ";
		$strSQL .= "	description = '".mb_strtoupper($_REQUEST["description"], "UTF-8")."', ";
		$strSQL .= "	codPos = '".mb_strtoupper($_REQUEST["codPos"], "UTF-8")."', ";
		$strSQL .= "	province = '".mb_strtoupper($_REQUEST["province"], "UTF-8")."', ";
		$strSQL .= "	latitude = ".$latitude.", ";
		$strSQL .= "	longitude = ".$longitude.", ";
		$strSQL .= "	kms = ".$_REQUEST["kmts"].", ";
		$strSQL .= "	weatherCode = ".$_REQUEST["weatherLocation"].", ";
		$strSQL .= "	state = '".$stateModify."'";
		$strSQL .= " WHERE idPtoSuelta = ". $_REQUEST["idPtoSuelta"];
	}
	
	$DB->Execute($strSQL);
	
	$arrJSON = new stdClass();
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = 'Ocurri&oacute; un error al grabar el archivo';
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = 'El registro se guard&oacute; correctamente';
	}	
	return json_encode($arrJSON);
}

function del()
{
	require("includes/constants.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	$DB->Execute("UPDATE ptosuelta SET state='$stateErase' WHERE idPtoSuelta=" . $_REQUEST["idPtoSuelta"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = 'Ocurri&oacute; un error al eliminar';
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = 'El registro se elimin&oacute; correctamente';
	}	
	return json_encode($arrJSON);
}

function editPtoSuelta()
{
	require("includes/constants.php");
		
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsPtoSuelta = $DB->Execute("SELECT * FROM ptosuelta WHERE state <> '$stateErase' and idPtoSuelta=" . $_REQUEST["idPtoSuelta"]); // execute query
						
	if(!$rsPtoSuelta->EOF){
		$signLat = 1;
		$signLng = 1;
				
		$latAbs = abs(round($rsPtoSuelta->fields("latitude") * 1000000.));
		$latDeg = (floor($latAbs / 1000000) * $signLat);		
		$latMin = floor((($latAbs/1000000) - floor($latAbs/1000000)) * 60);
		$latSeg = (floor((((($latAbs/1000000) - floor($latAbs/1000000)) * 60) - floor((($latAbs/1000000) - floor($latAbs/1000000)) * 60)) * 100000) *60/100000);
		
		$lngAbs = abs(round($rsPtoSuelta->fields("longitude") * 1000000.));
		$lngDeg = (floor($lngAbs / 1000000) * $signLat);		
		$lngMin = floor((($lngAbs/1000000) - floor($lngAbs/1000000)) * 60);
		$lngSeg = (floor((((($lngAbs/1000000) - floor($lngAbs/1000000)) * 60) - floor((($lngAbs/1000000) - floor($lngAbs/1000000)) * 60)) * 100000) *60/100000);
		
		$joPtoSuelta->data['description'] = $rsPtoSuelta->fields("description");
		$joPtoSuelta->data['codPos'] = $rsPtoSuelta->fields("codPos");
		$joPtoSuelta->data['province'] = $rsPtoSuelta->fields("province");
		$joPtoSuelta->data['latDeg'] = $latDeg;
		$joPtoSuelta->data['latMin'] = $latMin;
		$joPtoSuelta->data['latSeg'] = $latSeg;
		$joPtoSuelta->data['lngDeg'] = $lngDeg;
		$joPtoSuelta->data['lngMin'] = $lngMin;
		$joPtoSuelta->data['lngSeg'] = $lngSeg;
		$joPtoSuelta->data['kmts'] = $rsPtoSuelta->fields("kms");
		$joPtoSuelta->data['weatherCode'] = $rsPtoSuelta->fields("weatherCode");
		
		return json_encode($joPtoSuelta->data);
	}
	else
		return json_encode('');
}

switch($_REQUEST['actionOfForm'])
{
	case "EDIT":
		echo editPtoSuelta();
		break;
	case "DELETE":
		echo del();
		break;
	case "search":
		echo searchABMPtoSuelta();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;		
}
?>
